package reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

public class ReflectDemo4 {
    public static void main(String[] args) throws Exception {
        Person p = new Person();
        p.sayHello();

        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入一个完全限定名:");
            String name = sc.nextLine();
            //实例化
            Class cls = Class.forName(name);
            Object obj = cls.newInstance();
            //调用方法
            //1.获取待调用方法的方法对象
            while (!"exit".equals(name)) {
                System.out.println("请输入一个方法名:");
                name = sc.nextLine();
                Method method = cls.getMethod(name);//至少传入方法名
                method.invoke(obj);//obj.sayHello()
            }
        }
    }
}
